package demo1;

import java.util.Arrays;

class Student{
    public String name;
    public int age;

    public Student(String name,int age) {
        this.name = name;
        this.age=age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class Test1 {
    public static void main1(String[] args) {
        String str1 = "hello";
        String str2 = new String("abc");
        //将字符数组转化为字符串
        char[] chars = {'g','a','c'};
        String str3 = new String(chars);

        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
    }
    //拷贝
    public static void main2(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("world");
        String s3 = s1;
        System.out.println(s3);
        System.out.println(s2);
        System.out.println(s1.length());
    }
    //字符串对象的比较
    public static void main3(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("hello");
        //对于比较，不能简单的使用==来比较，需要用到equals方法
        System.out.println(s1.equals(s2));
        System.out.println(s1.equalsIgnoreCase(s2));//忽略大小写是否相同
        //比较大小
        //int ret = s1.compareTo(s2);//比较大小
        int ret = s1.compareToIgnoreCase(s2);//忽略大小写比较大小
        if(ret>0){
            System.out.println("s1>s2");
        }else if(ret==0){
            System.out.println("s1==s2");
        }else{
            System.out.println("s1<s2");
        }
    }
    //字符串查找
    public static void main4(String[] args) {
        String str = "abcdcfbc";
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i); //取到字符串中i的下标表示的值
            System.out.println(ch);
        }
        System.out.println(str.indexOf('c')); //返回c的下标，默认找一次出现的，打印2
        System.out.println(str.indexOf('c', 3)); //从3位置开始找，打印4
        System.out.println(str.indexOf("bc")); //找字符串起始位置的下标，打印1
        System.out.println(str.indexOf("bc",4)); //从4位置开始找字符串
        System.out.println("=====================");
        //倒着找
        System.out.println(str.lastIndexOf('c')); //返回c的下标，默认找一次出现的，打印2
        System.out.println(str.lastIndexOf('c', 3)); //从3位置开始找，打印4
        System.out.println(str.lastIndexOf("bc")); //找字符串起始位置的下标，打印1
    }
    //数值和字符串转化
    public static void main5(String[] args) {
        //把整数转为字符串
        String str1 = String.valueOf(123);
        System.out.println(str1);
        String str2 = String.valueOf(true);
        System.out.println(str2);
        String s4 = String.valueOf(new Student("Hanmeimei",18));
        System.out.println(s4);
        System.out.println("================");
        //字符串转为整数
        int val1 = Integer.parseInt("123"); //包装类
        System.out.println(val1+1); //打印124

        double val2 = Double.parseDouble("12.25");
        System.out.println(val2);
    }
    //大小写转换
    public static void main6(String[] args) {
        String str1 = "helloGT";
        String ret1 = str1.toUpperCase(); //将小写字母变成大写，原本就大写的不管
        System.out.println(ret1);

        String str2 = "helloGT";
        String ret2 = str2.toLowerCase(); //将小写字母变成大写，原本就大写的不管
        System.out.println(ret2);
        //字符串转数组
        char[] chars = str2.toCharArray();
        System.out.println(Arrays.toString(chars));
    }
    //字符串转换
    public static void main7(String[] args) {
        String str1 = "ababcabcdacadef";
        String ret = str1.replace('a','p');
        //将所有的a替换成p,返回一个新的字符串首字母地址
        System.out.println(ret);

        String ret2 = str1.replace("ab","pk");
        System.out.println(ret2);

        String ret3 = str1.replaceAll("abc","pkrg");
        System.out.println(ret3);

        String ret4 = str1.replaceFirst("abc","cba");
        //将第一个abc变成cba
        System.out.println(ret4);
    }
    //字符串拆分
    public static void main8(String[] args) {
        String str = "zhangsan&wangwu&zhaoliu&lisi";
        String[] ret = str.split("&",2);//分出来后是字符串数组，用下标将原来的字符串分开
        System.out.println(Arrays.toString(ret));

        System.out.println("====================");
        String str2 = "127.0.0.1";
        String[] ret2 = str2.split("\\.");//转义字符，两个\\转变成了一个\，然后\和点号再被转为.
        for (String x : ret2) {
            System.out.println(x);
        }

        System.out.println("====================");
        String str3 = "127\\0\\0\\1";
        String[] ret3 = str3.split("\\\\"); //两个都可以
        String[] ret4 = str3.split("/");// 一个/会被转为\\
        for (String x : ret4) {
            System.out.println(x);
        }

        System.out.println("====================");

        String str4 = "name=zhangsan&age=18";
        String[] ret5 = str4.split("=|&");//有多个连接符的情况
        for (String x : ret5) {
            System.out.println(x);
        }
        //多次拆分
        String[] ret6 = str4.split("&");
        for (String x : ret6) {
            //System.out.println(x);//name=zhangsan
            String[] ss = x.split("=");
            for(String s : ss) {
                System.out.println(s);
            }
        }
    }
    //字符串截取
    public static void main(String[] args) {
        String str = "helloworld";
        //String ret1 = str.substring(1); //从1下标开始截取到最后
        String ret1 = str.substring(0,5);
        // 字符串当中的库函数 基本上只要改变 返回的都是一个全新的对象
        System.out.println(ret1); //打印hello
    }
}
